package xcmg.device.service.announcement;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.announcement.BasicAnnouncementAuthDO;
import xcmg.device.dao.mapper.announcement.BasicAnnouncementAuthMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.StringUtil;
import xcmg.device.service.vo.announcement.BasicAnnouncementAuthAddVO;
import xcmg.device.service.vo.announcement.BasicAnnouncementAuthVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.List;

@Service
public class BasicAnnouncementAuthService {

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private BasicAnnouncementAuthMapper basicAnnouncementAuthMapper;

    public PageCommonVO listDealer(SearchCommonVO<BasicAnnouncementAuthVO> condition) {
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        condition.getFilters().setDealerOrgNameList(StringUtil.splitList(condition.getFilters().getDealerOrgNames()));
        condition.getFilters().setUserNameList(StringUtil.splitList(condition.getFilters().getUserNames()));
        List<BasicAnnouncementAuthVO> voList = basicAnnouncementAuthMapper.listDealer(condition.getFilters());
        for (BasicAnnouncementAuthVO authVO : voList) {
            authVO.setAuthType(0);
            authVO.setMainId(condition.getFilters().getMainId());
        }
        result.setPageInfo(new PageInfo(voList));
        result.setPageInfoList(voList);
        return result;
    }

    public PageCommonVO listUser(SearchCommonVO<BasicAnnouncementAuthVO> condition) {
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        condition.getFilters().setDealerOrgNameList(StringUtil.splitList(condition.getFilters().getDealerOrgNames()));
        condition.getFilters().setUserNameList(StringUtil.splitList(condition.getFilters().getUserNames()));
        List<BasicAnnouncementAuthVO> voList = basicAnnouncementAuthMapper.listUser(condition.getFilters());
        for (BasicAnnouncementAuthVO authVO : voList) {
            authVO.setAuthType(0);
            authVO.setMainId(condition.getFilters().getMainId());
        }
        result.setPageInfo(new PageInfo(voList));
        result.setPageInfoList(voList);
        return result;
    }

    public PageCommonVO listCustomer(SearchCommonVO<BasicAnnouncementAuthVO> condition) {
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<BasicAnnouncementAuthVO> voList = basicAnnouncementAuthMapper.listCustomer(condition.getFilters());
        for (BasicAnnouncementAuthVO authVO : voList) {
            authVO.setAuthType(0);
            authVO.setMainId(condition.getFilters().getMainId());
        }
        result.setPageInfo(new PageInfo(voList));
        result.setPageInfoList(voList);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode saveOpr(BasicAnnouncementAuthAddVO addVO, String userId, String userName) {
        List<BasicAnnouncementAuthVO> addList = addVO.getAddList();
        List<BasicAnnouncementAuthVO> removeList = addVO.getRemoveList();

        if (CollectionUtils.isNotEmpty(addList)) {
            for (BasicAnnouncementAuthVO authVO : addList) {
                BasicAnnouncementAuthDO authDO = new BasicAnnouncementAuthDO();
                BeanUtils.copyProperties(authVO, authDO);
                authDO.setId(fastGenClient.textGuid().getValue());
                authDO.setCreateId(userId);
                authDO.setCreateName(userName);
                basicAnnouncementAuthMapper.insert(authDO);
            }
        }

        if (CollectionUtils.isNotEmpty(removeList)) {
            List<String> idsList = new ArrayList<>();
            for (BasicAnnouncementAuthVO authVO : removeList) {
                idsList.add(authVO.getId());
            }
            basicAnnouncementAuthMapper.deleteByIds(idsList);
        }

        return ErrorCode.Success;
    }
}
